<?php
class Admin_Plugin_Bar extends Zend_Controller_Plugin_Abstract {

	private $_drop_down_items = array(
		array( 'label' => 'Просмотреть', 'name'=> 'all_list', 'icon' => 'icon-list' ),
		array( 'label' => 'Редактировать', 'name'=> 'edit_list', 'icon' => 'icon-pencil' )
	);
	
	public function postDispatch(Zend_Controller_Request_Abstract $request) {
		$acl = Zen_Acl::getInstance();
		if (! Zen_User::isLoged ()) 
			return ;
		$user = Zen_User::getCurrent ();
		if (! $acl->isAllowed ( $user->role, 'adminka' )) 
			return ;
		if ( $this->getRequest ()->isXmlHttpRequest ()) 
			return ;
		$route = Zend_Controller_Front::getInstance ()->getRouter ()->getCurrentRouteName ();
		if ( in_array($route, array('admin','backend','login','logout','profile')) ) 
			return ;
		$response = $this->getResponse ();
		$body = $response->getBody ();
		$layout = Zend_Layout::getMvcInstance ();
		$view = $layout->getView ();
		
		$drop_down_html = $this->_renderDropDownItems();
		
		$buttons = "";
		$url = $_SERVER ['REQUEST_URI'];
		if ($acl->isAllowed ( $user->role, 'seo' )) {
			$seoButton = $view->action ( 'button', 'bar', 'seo', array ('url' => $url ) );
			$buttons .= $seoButton;
		}
		Zen_Style::add ( '/cms/application/modules/admin/res/css/bar.css',100 );
		//Zen_Script::add ( '/cms/application/modules/admin/res/js/bootstrap-dropdown.js',100 );
		//Zen_Script::add ( '/cms/application/modules/admin/res/js/bar.js',100 );
		$cnf=Zend_Registry::get('cnf');
		$sitename = $cnf->sitename;
		
		if ($acl->isAllowed ( $user->role, 'seo' )) {
			$seoButton = $view->action ( 'button', 'bar', 'seo', array ('url' => $url ) );
			$drop_down_html .= '<li>' . $seoButton . '</li>';
		}
		
		$bar = "
			<div id='zen_bar' class='navbar navbar-fixed-top'>
			  <div class='navbar-inner'>
			    <div class='container'>
			      <a class='brand' href='#'>" . $cnf->sitename . "</a>
			      <ul class='nav'>
						$drop_down_html
			      </ul>
			      <div class='clearfix' style='float: left; padding-top: 10px; padding-left: 270px;'>
					<div style='float: left; '><a style='color: #999;' href='/admin/'>Администрирование</a></div>
					<a href='/admin/' class='tumbler-bar clearfix'>
						<span class='tumbler'></span>
					</a>
					<div style='float: left; color: #fff;'>Просмотр сайта</div>
			      </div>
			      <div class='nav-collapse' style='float:right;'>
			        <ul class='nav'>
			          <li><a href='/profile' ><i class='icon-user icon-grey '></i>&nbsp;". $user->name . "</a></li>
			          <li><a href='/logout/'><i class='icon-share-alt icon-grey '></i>&nbsp;Выход</a></li>
			        </ul>
			      </div><!--/.nav-collapse -->
			    </div>
			  </div>
			</div>
			";
		$layout->bar = $bar;
	}
	
	private function _renderDropDownItems() {
		$drop_down_items = $this->_drop_down_items;
		$layout = Zend_Layout::getMvcInstance ();
		$view = $layout->getView ();
		$html = "";
		foreach ($drop_down_items as $item) {
			$list = $view->placeholder($item['name'])->getValue();
			if (! $list) continue;
			$html .= "" ;
			if (count ($list) == 1) { 
				$list_item = $list;
				unset($list);
				$list[] = $list_item;
			} 
			$html .= "<li class='dropdown'><a class='dropdown-toggle' data-toggle='dropdown' href='#'><i class='" . $item['icon'] . " icon-grey'></i>&nbsp;" . $item['label'] . "&nbsp;<b class='caret'></b></a>";
			$html .= "<ul class='dropdown-menu'>";
			$list = array_reverse($list);
			foreach ($list as $value) {
				list ($key, $value) = each($value);
				$html .= "<li>";
				$html .= "<a href='$value'>$key</a>";
				$html .= "</li>";
			}
			$html .= "</ul></li>";
		}
		return $html;
	}

}